#ifndef SMPP_HELP_FUNC_H
#define SMPP_HELP_FUNC_H

#include <QtCore/QTextCodec>
#include "smpp/parameters/smpp_prameters_mandatory.h"

namespace smpp {

namespace helpers {

static const quint32 UDH_LEN = 0x00000006;

struct SMSUDH {
  quint8 sequence_number;
  quint8 length;
  quint8 position;
};

bool getUdh(SMSUDH &udh, const QByteArray &b_data) {
  if((quint32)b_data.size() < UDH_LEN)
    return false;

  if(b_data[0] == 0x05 && b_data[1] == 0x00 && b_data[2] == 0x03) {
    udh.sequence_number = b_data[3];
    udh.length = b_data[4];
    udh.position = b_data[5];
    return true;
  } else {
    return false;
  }
}

QByteArray toDataCodes(const QByteArray &b_data, quint8 codes) {
  QString data(QString::fromUtf8(b_data));
  QTextCodec *codec(0);
  switch(codes) {
    case smpp::parameters::data_coding::ASCII:
      return data.toAscii();
    case smpp::parameters::data_coding::BIT8_2:
    case smpp::parameters::data_coding::BIT8_4:
      return data.toLocal8Bit();
    case smpp::parameters::data_coding::LATIN_1:
      return data.toLatin1();
    case smpp::parameters::data_coding::JIS:
      codec = QTextCodec::codecForName("JIS X 0208");
      break;
    case smpp::parameters::data_coding::CYRLLIC:
      codec = QTextCodec::codecForName("ISO 8859-5");
      break;
    case smpp::parameters::data_coding::HUBREW:
      codec = QTextCodec::codecForName("ISO 8859-8");
      break;
    case smpp::parameters::data_coding::UCS2:
      codec = QTextCodec::codecForName("UTF-16");
      break;
    case smpp::parameters::data_coding::PICTOGRAM:
      codec = QTextCodec::codecForName("Shift-JIS");
      break;
    case smpp::parameters::data_coding::MUSIC:
      codec = QTextCodec::codecForName("ISO 2022-JP");
      break;
    case smpp::parameters::data_coding::SMSC_DEFAULT:
    default:
      return data.toUtf8();
  }

  return codec->fromUnicode(data);
}

QByteArray fromDataCodes(const QByteArray &b_data, quint8 codes) {
  QTextCodec *codec(0);
  switch(codes) {
    case smpp::parameters::data_coding::ASCII:
      return QString::fromAscii(b_data).toUtf8();
    case smpp::parameters::data_coding::BIT8_2:
    case smpp::parameters::data_coding::BIT8_4:
      return QString::fromLocal8Bit(b_data).toUtf8();
    case smpp::parameters::data_coding::LATIN_1:
      return QString::fromLatin1(b_data).toUtf8();
    case smpp::parameters::data_coding::JIS:
      codec = QTextCodec::codecForName("JIS X 0208");
      break;
    case smpp::parameters::data_coding::CYRLLIC:
      codec = QTextCodec::codecForName("ISO 8859-5");
      break;
    case smpp::parameters::data_coding::HUBREW:
      codec = QTextCodec::codecForName("ISO 8859-8");
      break;
    case smpp::parameters::data_coding::UCS2:
      codec = QTextCodec::codecForName("UTF-16");
      break;
    case smpp::parameters::data_coding::PICTOGRAM:
      codec = QTextCodec::codecForName("Shift-JIS");
      break;
    case smpp::parameters::data_coding::MUSIC:
      codec = QTextCodec::codecForName("ISO 2022-JP");
      break;
    case smpp::parameters::data_coding::SMSC_DEFAULT:
    default:
      return b_data;
  }

  return codec->toUnicode(b_data).toUtf8();
}

QByteArray convertDataBetweenCodes(quint8 from_codes,
                                   quint8 to_codes,
                                   const QByteArray &from_data) {
  return toDataCodes(fromDataCodes(from_data, from_codes), to_codes);
}

} // namespace helpers

} // namespace smpp


#endif // SMPP_HELP_FUNC_H
